package dev.bong.controller;

import dev.bong.dto.ScheduleDTO;
import dev.bong.dto.SemesterDTO;
import dev.bong.dto.UserDTO;
import dev.bong.dto.UserPageQueryDTO;

import dev.bong.pojo.entity.User;
import dev.bong.pojo.result.PageResult;
import dev.bong.pojo.result.Result;


import dev.bong.service.AdminService;
import dev.bong.vo.ApplyVo;
import dev.bong.vo.LabVo;
import dev.bong.vo.RegisterVO;
import dev.bong.vo.SemesterVo;
import dev.bong.vo.schedule.ScheduleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/admin")
@Api(tags = "管理员模块")
public class AdminController {

    @Autowired
    private AdminService adminService;



    @GetMapping("/semester/list")
    @ApiOperation("a-获取学期列表")
    public Result<List<SemesterVo>> getSemesterList(){

        return Result.success(adminService.getSemesterList());
    }

    @PostMapping("/semester/add")
    @ApiOperation("a-添加学期")
    public Result addSemester(SemesterDTO dto){

        adminService.addSemester(dto);
        return Result.success("添加学期，成功！");
    }
    @PostMapping("/semester/current")
    @ApiOperation("a-设置当前学期")
    public Result setCurrentSemester(Integer semesterId){

        adminService.setCurrentSemester(semesterId);
        return Result.success("设置当前学期，成功！");
    }


    @PostMapping("/user/register")
    @ApiOperation("b-注册用户")
    public Result register(@RequestBody @Validated UserDTO userDTO) {

        User user= adminService.findByUserAccount(userDTO.getUserAccount());

        if(user!=null){
            return Result.error("该账号已存在");
        }
        adminService.register(userDTO);

        return Result.success("注册成功");
    }

    @PostMapping("user/delete")
    @ApiOperation("b-删除用户")
    public Result deleteUser(@RequestBody List<Integer> ids){

        adminService.deleteUser(ids);
        return Result.success("删除用户成功");
    }

    @PutMapping("/user/update")
    @ApiOperation("b-修改用户")
    public Result updateUser(@RequestBody UserDTO userDTO){

        Integer userId=userDTO.getId();
        adminService.updateUser(userDTO,userId);
        return Result.success();
    }
    @PutMapping("/user/reset")
    @ApiOperation("b-重置密码")
    public Result resetPassword(@RequestBody List<Integer> ids){

        adminService.resetPassword(ids,"000000");
        return Result.success();

    }

    @GetMapping("/page")
    @ApiOperation("b-分页查询用户")
    public Result<PageResult> page(UserPageQueryDTO dto){

        log.info("分页查询用户,参数为：{}",dto);
        PageResult pageResult= adminService.pageQuery(dto);
        log.info("controller");
        return Result.success(pageResult);
    }

    @GetMapping("/schedule/list")
    @ApiOperation("c-实验室排课表")
    public Result<List<ScheduleVo>> getScheduleList(){
        List<ScheduleVo> ans=adminService.getScheduleList();
        return Result.success(ans);
    }

    @PostMapping("/schedule/add")
    @ApiOperation("c-实验室排课表--新增")
    public Result addSchedule(@RequestBody ScheduleDTO dto) throws Exception {

        adminService.addSchedule(dto);

        return Result.success("新增排课，成功！");
    }


    @GetMapping("/register/list")
    @ApiOperation(value = "c-实验室登记列表",notes = "申请状态: -1:未排课，1:已排课")
    public Result<List<RegisterVO>> getRegisterList(){
        List<RegisterVO> ans=adminService.getRegisterList();
        return Result.success(ans);
    }




    @GetMapping("/borrow/list")
    @ApiOperation(value = "实验室借用列表",notes = "审批状态：-1:未审核，0:驳回，1:通过，2:使用完毕")
    public Result<List<ApplyVo>>  getBorrowList() {

        List<ApplyVo> list=adminService.getBorrowList();
        return Result.success(list);
    }

    @PostMapping("/borrow/approval")
    @ApiOperation(value = "实验室借用申请审批",notes = "审批状态：-1:未审核，0:驳回，1:通过，2:使用完毕")
    public Result approvalBorrow(Integer applyId,Integer applyStatus){

        adminService.approvalBorrow(applyId,applyStatus);
        return Result.success("审批完成");
    }

    @GetMapping("/lab/list")
    @ApiOperation(value = "实验室列表")
    public Result<List<LabVo>> getLabList(){

        List<LabVo> ans=adminService.getLabList();
        return Result.success(ans);
    }





}
